package com.android.phoenix.activity.discovery;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TimePicker;

import com.android.phoenix.R;
import com.android.phoenix.activity.BaseActivity;
import com.android.phoenix.bean.ActivityInfo;
import com.android.phoenix.util.DataBaseUtil;

public class ReleaseActivity extends BaseActivity{
	
	private ImageView imgBack;
	
	private DataBaseUtil dbUtil;
	
	/**
	 * 开始日期
	 */
	private TextView tvStartDate;
	/**
	 * 开始时间
	 */
	private TextView tvStartTime;
	/**
	 * 活动总时间
	 */
	private EditText etActivityTime;
	/**
	 * 起始地址
	 */
	private EditText etStartAddress;
	/**
	 * 结束地址
	 */
	private EditText etEndAddress;
	/**
	 * 活动类型
	 */
	private TextView tvActivityType;
	/**
	 * 活动联系人
	 */
	private EditText etActivityContact;
	/**
	 * 联系人号码
	 */
	private EditText etContactNum;
	/**
	 * 活动描述
	 */
	private EditText etDetailDesc;
	
	/**
	 * 发布活动
	 */
	private Button btnRelease;
	
	private String startDate = "";
	
	private String startTime = "";
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.acti_release);
		imgBack = (ImageView) findViewById(R.id.imgBack);
		imgBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		tvStartDate = (TextView) findViewById(R.id.tvStartDate);
		tvStartDate.setOnClickListener(CheckStartDateListener);
		
		tvStartTime = (TextView) findViewById(R.id.tvStartTime);
		tvStartTime.setOnClickListener(CheckStartTimeListener);
		
		etActivityTime = (EditText) findViewById(R.id.etActivityTime);
		
		etStartAddress = (EditText) findViewById(R.id.etStartAddress);
		etEndAddress = (EditText) findViewById(R.id.etEndAddress);
		
		tvActivityType = (TextView) findViewById(R.id.tvActivityType);
		tvActivityType.setOnClickListener(activityTypeChangeListener);
		
		etActivityContact = (EditText) findViewById(R.id.etActivityContact);
		etContactNum = (EditText) findViewById(R.id.etContactNum);
		etDetailDesc = (EditText) findViewById(R.id.etDetailDesc);
		
		btnRelease = (Button) findViewById(R.id.btnRelease);
		btnRelease.setOnClickListener(ReleaseListener);
	}
	
private OnClickListener activityTypeChangeListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			final String[] content = new String[] { "训练", "长途跋涉", "运动交友", "旅游观光"};
			AlertDialog.Builder builder = new AlertDialog.Builder(ReleaseActivity.this);
			builder.setItems(content, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					tvActivityType.setText(content[which]);
				}
			});
			builder.show();
		}
	};
	
private OnClickListener CheckStartDateListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			StartDateDialog dialog = new StartDateDialog(ReleaseActivity.this);
			dialog.show();
		}
	};
	
private OnClickListener CheckStartTimeListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			StartTimeDialog dialog = new StartTimeDialog(ReleaseActivity.this);
			WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
			params.width = 700;
			dialog.show();
		}
	};
	
private OnClickListener ReleaseListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			dbUtil = new DataBaseUtil(ReleaseActivity.this);
			
			btnRelease = (Button) findViewById(R.id.btnRelease);
			btnRelease.setOnClickListener(ReleaseListener);
			startDate = tvStartDate.getText().toString().trim();
			startTime = tvStartTime.getText().toString().trim();
			String activityTime = etActivityTime.getText().toString().trim();
			String startAddress = etStartAddress.getText().toString().trim();
			String endAddress = etEndAddress.getText().toString().trim();
			String activtyType = tvActivityType.getText().toString().trim();
			String activityContact = etActivityContact.getText().toString().trim();
			String contactNumber = etContactNum.getText().toString().trim();
			String activityDesc = etDetailDesc.getText().toString().trim();
			
			ActivityInfo info = new ActivityInfo(startDate, startTime, activityTime, startAddress, endAddress, 
					activtyType, activityContact, contactNumber, activityDesc);
			dbUtil.addActivity(info);
			
			Intent intent = new Intent();
			intent.setClass(ReleaseActivity.this, BycecleActivity.class);
			startActivity(intent);
			finish();
		}
	};
	
	private class StartDateDialog extends Dialog{
		private static final int theme = R.style.Dialog;

		public StartDateDialog(Context context) {
			super(context, theme);
			setContentView(R.layout.dialog_date);
			Button btnOk = (Button)findViewById(R.id.btnOk);
			final DatePicker date = (DatePicker) findViewById(R.id.date);
			btnOk.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					tvStartDate.setText(date.getYear() + "-" + (date.getMonth() + 1) + "-" + date.getDayOfMonth());
					startDate = date.getYear() + "-" + (date.getMonth() + 1) + "-" + date.getDayOfMonth();
					dismiss();
				}
			});
			
			Button btnCancle = (Button)findViewById(R.id.btnCancel);
			btnCancle.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					dismiss();
				}
			});
		}
	}
	
	private class StartTimeDialog extends Dialog{
		private static final int theme = R.style.Dialog;

		public StartTimeDialog(Context context) {
			super(context, theme);
			setContentView(R.layout.dialog_time);
			final TimePicker timePicker = (TimePicker) findViewById(R.id.timePicker);
			timePicker.setIs24HourView(true);
			Button btnOk = (Button)findViewById(R.id.btnOk);
			btnOk.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					startTime = timePicker.getCurrentHour() + ":" + timePicker.getCurrentMinute();
					tvStartTime.setText(startTime);
					dismiss();
				}
			});
			
			Button btnCancle = (Button)findViewById(R.id.btnCancel);
			btnCancle.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					dismiss();
				}
			});
		}
	}
		

}
